package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.pojo.DtsOrder;
import com.qf.service.DtsOrderService;
import com.qf.vo.CatVo;
import com.qf.vo.OrderVo;
import com.qf.vo.ShipVo;
import com.qf.vo.StatVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 11:38
 */
@Service
public class DtsOrderServiceImpl implements DtsOrderService {
    @Autowired
    private DtsOrderMapper dtsOrderMapper;
    @Override
    public Page<DtsOrder> selectPage(Integer page, Integer limit, String order, String sort, Integer[] orderStatusArray, String orderSn) {
        Page<DtsOrder> dtsOrderPage = new Page<>(page,limit);
        QueryWrapper<DtsOrder> dtsOrderQueryWrapper = new QueryWrapper<>();
        dtsOrderQueryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort).like(!StringUtils.isEmpty(orderSn),"order_sn",orderSn).in(orderStatusArray!=null,"order_status",orderStatusArray).eq("deleted",false);
        return dtsOrderMapper.selectPage(dtsOrderPage,dtsOrderQueryWrapper);
    }

    @Override
    public Set<ShipVo> selectlistShipChannel() {
        //List<DtsOrder> ship_channel = dtsOrderMapper.selectList(new QueryWrapper<DtsOrder>().select("ship_channel"));
        List<DtsOrder> ship_channel = dtsOrderMapper.selectList(null);
        Set<String> collect = ship_channel.stream().map(DtsOrder::getShipChannel).collect(Collectors.toSet());
//        {
//            "value": "HTKY",
//                "label": "百世快递"
//        },
//        {
//            "value": "ZTO",
//                "label": "中通快递"
//        },
//        {
//            "value": "YTO",
//                "label": "圆通速递"
//        },
//        {
//            "value": "YD",
//                "label": "韵达速递"
//        }
//		]
        Set<ShipVo> shipVos = new HashSet<>();
        if(collect.contains("HTKY")){
           ShipVo shipVo = new ShipVo();
           shipVo.setValue("HTKY");
           shipVo.setLabel("百世快递");
           shipVos.add(shipVo);
       }
        if(collect.contains("邮政小包")){
            ShipVo shipVo = new ShipVo();
            shipVo.setValue("邮政小包");
            shipVo.setLabel("邮政小包");
            shipVos.add(shipVo);
        }
        if(collect.contains("邮政国内小包")){
            ShipVo shipVo = new ShipVo();
            shipVo.setValue("邮政国内小包");
            shipVo.setLabel("邮政国内小包");
            shipVos.add(shipVo);
        }
        if(collect.contains("ZTO")){
            ShipVo shipVo = new ShipVo();
            shipVo.setValue("ZTO");
            shipVo.setLabel("中通快递");
            shipVos.add(shipVo);
        }
        if(collect.contains("YTO")){
            ShipVo shipVo = new ShipVo();
            shipVo.setValue("YTO");
            shipVo.setLabel("圆通速递");
            shipVos.add(shipVo);
        }
        if(collect.contains("YD")){
            ShipVo shipVo = new ShipVo();
            shipVo.setValue("YD");
            shipVo.setLabel("韵达速递");
            shipVos.add(shipVo);
        }
        return shipVos;
    }

    @Override
    public void updateOrder(DtsOrder dtsOrder) {
        dtsOrderMapper.updateById(dtsOrder);
    }

    @Override
    public DtsOrder selectById(Integer id) {
        return dtsOrderMapper.selectById(id);
    }

    @Override
    public StatVo selectCount() {
        /*
        "columns": [
			"day",
			"orders",
			"customers",
			"amount",
			"pcr"
		],
         */
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
       List<OrderVo>  orderVoList= dtsOrderMapper.selectOrderVo();
        String[] strings = {"day", "orders", "customers", "amount", "pcr"};
        ArrayList<Map> maps = new ArrayList<>();
        for (OrderVo orderVo : orderVoList) {
            HashMap hashMap = new HashMap();
            hashMap.put("day",df.format(orderVo.getDay()));
            hashMap.put(  "orders",orderVo.getOrders());
            hashMap.put( "customers",orderVo.getCustomers());
            hashMap.put("amount",orderVo.getAmount());
            hashMap.put("pcr",orderVo.getPcr());
            maps.add(hashMap);
        }
        StatVo statVo = new StatVo();
        statVo.setColumns(strings);
        statVo.setRows(maps);
        return statVo;
    }


}
